﻿using System;
using StudentDB;
using System.Linq;
using System.Collections.Generic;
using StudentDB;

namespace StudentGradeDAL
{
    class Program
    {
        static void Main(string[] args)
        {
            StudentDAL sd = new StudentDAL();
            foreach (Student x in sd.GetAllStudents())
            {
                Console.WriteLine($"学号为{x.Sno},姓名是{x.Sname}");
            }
            Console.WriteLine("  ");
            Student s = new Student();
            s.Sno = "202100925";
            s.Sname = "shangjiakun";
            sd.InsertStudent(s);
            foreach (Student x in sd.GetAllStudents())
            {
                Console.WriteLine($"学号为{x.Sno},姓名是{x.Sname}");
            }
            Console.WriteLine("  ");
            sd.DeleteStudent(s);
            foreach (Student x in sd.GetAllStudents())
            {
                Console.WriteLine($"学号为{x.Sno},姓名是{x.Sname}");
            }
        }
    }
    public class StudentDAL
    {
        public bool InsertStudent(Student s)
        {
            try
            {
                using (StudentDBContext db = new StudentDBContext())
                {
                    db.Students.Add(s);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool DeleteStudent(Student s)
        {
            try
            {
                using (StudentDBContext db = new StudentDBContext())
                {
                    var temp = db.Students.Find(new object[] { s.Sno });
                    db.Students.Remove(temp);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool UpdateStudent(Student olds, Student news)
        {
            try
            {
                using (StudentDBContext db = new StudentDBContext())
                {
                    var temp = db.Students.Find(new object[] { olds.Sno });
                    temp.Sname = news.Sname;
                    temp.Ssex = news.Ssex;
                    temp.Sage = news.Sage;
                    temp.Sdeptno = news.Sdeptno;
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public List<Student> GetAllStudents()
        {
            try
            {
                using (StudentDBContext db = new StudentDBContext())
                {
                    return db.Students.ToList<Student>();
                }
            }
            catch
            {
                return null;
            }
        }
        public Student SelectStudent(string _sno)
        {
            try
            {
                using (StudentDBContext db = new StudentDBContext())
                {
                    var temp = (
                               from s in db.Students
                               where s.Sno == _sno
                               select s
                               ).FirstOrDefault();

                    return temp;
                }
            }
            catch
            {
                return null;
            }
        }
        public List<Student> SelectStudents(string _name)
        {
            try
            {
                using (StudentDBContext db = new StudentDBContext())
                {
                    var temp = (from s in db.Students
                                where s.Sname.IndexOf(_name) > 0
                                select s).ToList<Student>();

                    return temp;
                }
            }
            catch
            {
                return null;
            }
        }
    }
}